package com.example.dllo.moondog.information.newsss;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.moondog.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 16/3/2.
 */
public class NewAdapter extends BaseAdapter {
    private List<NewBeans.ListEntity> data;
    private Context context;

    public NewAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<NewBeans.ListEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size() > 0 && data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            myViewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_new, null);
            myViewHolder.title = (TextView) convertView.findViewById(R.id.tv_new_title);
            myViewHolder.summary = (TextView) convertView.findViewById(R.id.tv_new_summary);
            myViewHolder.time = (TextView) convertView.findViewById(R.id.tv_new_time);
            myViewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_new);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.title.setText(data.get(position).getTitle());
        myViewHolder.summary.setText(data.get(position).getSummary());
        myViewHolder.time.setText(data.get(position).getInsert_date());

        Picasso.with(context).load(data.get(position).getImage_url_small()).into(myViewHolder.imageView);

//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView, R.mipmap.holder, R.mipmap.holder);
//        VolleySingleton.getImageLoader().get(data.get(position).getImage_url_small(), listener);

        return convertView;
    }

    private class MyViewHolder {
        private TextView title, summary, time;
        private ImageView imageView;
    }
}
